package FSMDE.models.tests;

import java.util.UUID;

import junit.framework.Assert;

import main.java.ERMDE.models.Attribute;
import main.java.ERMDE.models.AttributeCollection;
import main.java.ERMDE.models.Diagram;
import main.java.ERMDE.models.Entity;
import main.java.ERMDE.models.EntityCollection;
import main.java.FSMDE.models.ComplexComponent;
import main.java.FSMDE.models.Component;
import main.java.FSMDE.models.ComponentType;
import main.java.FSMDE.models.FactoryComponent;
import main.java.FSMDE.models.RelatedComponent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class ComponentTestCase {

	@Test
	public void testCreatingComponents() {
		Component component = Component
				.create("TestName", ComponentType.Button);

		Assert.assertEquals("TestName", component.getName());
		Assert.assertEquals(ComponentType.Button, component.getType());
		Assert.assertTrue(component.getId() instanceof UUID);
		Assert.assertTrue(component instanceof Component);

		component = Component.create("TestName1", ComponentType.Form);

		Assert.assertEquals("TestName1", component.getName());
		Assert.assertEquals(ComponentType.Form, component.getType());
		Assert.assertTrue(component.getId() instanceof UUID);
		Assert.assertTrue(component instanceof ComplexComponent);
	}

	@Test
	public void testComponentsAddAndRemoveToComplexComponent() {
		ComplexComponent section = (ComplexComponent) Component.create(
				"TestName", ComponentType.Section);
		Assert.assertEquals(0, section.getComponents().count());

		section.getComponents().add(
				Component.create("TestName1", ComponentType.Button));
		Assert.assertEquals(1, section.getComponents().count());

		section.getComponents().add(
				Component.create("TestName2", ComponentType.Text));
		Assert.assertEquals(2, section.getComponents().count());

		ComplexComponent form = (ComplexComponent) Component.create(
				"TestName3", ComponentType.Form);
		section.getComponents().add(form);
		Assert.assertEquals(3, section.getComponents().count());

		section.getComponents().remove(form);
		Assert.assertEquals(2, section.getComponents().count());
	}

	@Test
	public void testRelatedComponents() {
		main.java.ERMDE.models.Diagram diagram = new Diagram(UUID.fromString("0-0-0-0-1"));

		AttributeCollection attributes01 = new AttributeCollection();
		AttributeCollection attributes02 = new AttributeCollection();

		Attribute attribute01 = new Attribute("att01");
		Attribute attribute02 = new Attribute("att02");
		Attribute attribute03 = new Attribute("att03");

		try {
			attributes01.addAttribute(attribute01);
			attributes02.addAttribute(attribute02);
			attributes02.addAttribute(attribute03);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Entity entity01 = new Entity("ent01");
		Entity entity02 = new Entity("ent02");

		entity01.setAttributes(attributes01);
		entity02.setAttributes(attributes02);

		EntityCollection entities = new EntityCollection();
		entities.add(entity01);
		entities.add(entity02);
		diagram.setEntities(entities);

		RelatedComponent form01 = (RelatedComponent) FactoryComponent.create(
				"form01", ComponentType.Form);
		RelatedComponent form02 = (RelatedComponent) FactoryComponent.create(
				"form02", ComponentType.Form);

		// act & validate
		Assert.assertEquals(0, form01.getComponents().count());
		try {
			form01.setRelationToErmAttribute(diagram, entity01, entity01
					.getAttributes().getAttribute("att01"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Assert.assertEquals(1, form01.getComponents().count());
		try {
			form01.setRelationToErmAttribute(diagram, entity02, entity02
					.getAttributes().getAttribute("att02"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Assert.assertEquals(2, form01.getComponents().count());
		try {
			form01.setRelationToErmAttribute(diagram, entity02, entity02
					.getAttributes().getAttribute("att03"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Assert.assertEquals(3, form01.getComponents().count());

		Assert.assertEquals(0, form02.getComponents().count());
		try {
			form02.setRelationToErmEntity(diagram, entity02);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Assert.assertEquals(2, form02.getComponents().count());

		Assert.assertNotNull(form01.getComponents().get(
				entity01.getAttributes().getAttribute("att01").getName()));
		Assert.assertNotNull(form01.getComponents().get(
				entity02.getAttributes().getAttribute("att02").getName()));
		Assert.assertNotNull(form01.getComponents().get(
				entity02.getAttributes().getAttribute("att03").getName()));

		Assert.assertNull(form02.getComponents().get(
				entity01.getAttributes().getAttribute("att01").getName()));
		Assert.assertNotNull(form02.getComponents().get(
				entity02.getAttributes().getAttribute("att02").getName()));
		Assert.assertNotNull(form02.getComponents().get(
				entity02.getAttributes().getAttribute("att03").getName()));

		form01.removeRelationWithErmModel(form01.getComponents().get(
				entity01.getAttributes().getAttribute("att01").getName()));
		Assert.assertEquals(2, form01.getComponents().count());
		Assert.assertNull(form01.getComponents().get(
				entity01.getAttributes().getAttribute("att01").getName()));
		form01.removeRelationWithErmModel(form01.getComponents().get(
				entity02.getAttributes().getAttribute("att02").getName()));
		Assert.assertEquals(1, form01.getComponents().count());
		Assert.assertNull(form01.getComponents().get(
				entity02.getAttributes().getAttribute("att02").getName()));
		Assert.assertNotNull(form01.getComponents().get(
				entity02.getAttributes().getAttribute("att03").getName()));
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
}
